package com.example.springboot.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Announcements;
import com.example.springboot.service.IAnnouncementsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 公告表 API控制器 - 专门用于不需要token验证的API
 * </p>
 *
 * @author
 */
@RestController
@RequestMapping("/api/announcements")
public class ApiAnnouncementsController {

    @Resource
    private IAnnouncementsService iAnnouncementsService;

    @GetMapping("/list")
    public Result list() {
        return Result.success(iAnnouncementsService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(iAnnouncementsService.getById(id));
    }

    // 可以添加其他不需要token验证的API方法


    //前端公告列表，分页返回
    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String type,
                              @RequestParam Integer page,
                              @RequestParam Integer pageSize) {
        QueryWrapper<Announcements> queryWrapper = new QueryWrapper<>();
        //筛选条件
        queryWrapper.orderByDesc("id");
        if (!"".equals(type)) {
            queryWrapper.like("type", type);
        }
        return Result.success(iAnnouncementsService.page(new Page<>(page, pageSize), queryWrapper));
    }
}
